package com.yoga.controller;

import com.yoga.entity.YogaCoach;
import com.yoga.entity.YogaPrivaterequest;
import com.yoga.entity.YogaUser;
import com.yoga.entity.YogaVenue;
import com.yoga.service.CoachService;
import com.yoga.service.VenueService;
import com.yoga.service.YogaUserService;
import com.yoga.utils.ResultVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class VenueController {

    @Resource
    private VenueService venueService;
    @Resource
    private YogaUserService yogaUserService;
    @Resource
    private CoachService yogaCoachService;

    @RequestMapping("showAllCoach")
    public ResultVO showAllCoach(Integer vid){
        System.out.println("收到了请求");

        List<YogaCoach> coachList = venueService.findAllCoach(vid);
        List<Integer> uidList = null;
        for (YogaCoach yogaCoach:coachList) {
            Integer uid = yogaCoach.getUid();
            uidList.add(uid);
        }
        List<YogaUser> userList = null;
        for (Integer uid:uidList) {
            YogaUser user = yogaUserService.findOne(uid);
            userList.add(user);
        }

        return ResultVO.success("查询所有教练成功",userList);
    }

    @RequestMapping("getVenueByUid")
    public ResultVO getVenueByUid(Integer uid) {
        YogaVenue yogaVenue = venueService.getVenueByUid(uid);
        return ResultVO.success("查询成功",yogaVenue);
    }

    @RequestMapping("showAllPrivate")
    public ResultVO showAllPrivate(){
        List<YogaPrivaterequest> list = venueService.findAllPrivaterequest();
        return ResultVO.success("查询所有私教请求成功",list);
    }

    @RequestMapping("addCoach")
    public ResultVO addCoach(@RequestBody YogaCoach yogaCoach){
        System.out.println("收到了请求");

        Boolean flag = venueService.addCoach(yogaCoach);
        if(!flag) {
            return ResultVO.fail("新增教练失败");
        }
        return ResultVO.success("新增教练成功");
    }

    @RequestMapping("addCoachNoUser")
    public ResultVO addCoachNoUser(@RequestBody YogaUser yogaUser){
        Boolean flag = yogaUserService.addUser(yogaUser);
        if(flag){
            YogaUser user = yogaUserService.findUserByPhone(yogaUser.getPhone());
            YogaCoach yogaCoach = new YogaCoach();
            yogaCoach.setUid(user.getUid());

        }
        return ResultVO.fail("新增失败");
    }

    @RequestMapping("findAllVenue")
    public ResultVO findAllVenue(){
        List<YogaVenue> list = venueService.findAllVenue();
        return ResultVO.success("查询成功",list);
    }

    @RequestMapping("findVenueByAddress")
    public ResultVO findVenueByAddress(String address){
        List<YogaVenue> list = venueService.findSomeVenue(address);
        return ResultVO.success("查询成功",list);
    }

    @RequestMapping("findOneVenue")
    public ResultVO findOneVenue(String address){
        YogaVenue venue = venueService.findOneVenue(address);
        return ResultVO.success("查询成功",venue);
    }

    @RequestMapping("findAllUser")
    public ResultVO findAllUser(){
        List<YogaUser> userList = venueService.findAllUser();
        return ResultVO.success("查询成功",userList);
    }

    @RequestMapping("findThisVenue")
    public ResultVO findThisVenue(Integer vid){
        YogaVenue venueByVid = venueService.findVenueByVid(vid);
        System.out.println("登陆尝试请求发送");
        return ResultVO.success("登陆成功",venueByVid);
    }


    @RequestMapping("deleteCoach")
    public ResultVO deleteeCoach(Integer uid){
        Boolean flag = venueService.deleteCoach(uid);
        if(flag){
//            离职
            return ResultVO.success("离职成功");
        }
        return ResultVO.fail("离职失败");
    }

    @RequestMapping("resetUserPwd")
    public ResultVO resetUserPwd(Integer uid){
        Boolean flag = venueService.resetUserPwd(uid);
        if(flag){
            return ResultVO.success("重置密码成功");
        }
        return ResultVO.fail("重置密码失败");
    }

}
